home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech's Sprocket™ / SprocketGX / Lib / AppleEventHandling.cp < prev    next >
Encoding:
Text File  |  1994-10-10  |  7.0 KB  |  232 lines  |  [TEXT/MMCC]

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll need to do ALOT more work in here.
  8.  
  9.     Written by: Dave Falkenburg
  10.  
  11.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  12.  
  13.     Change History (most recent first):
  14.      
  15.  */
  16.  
  17. #include <Types.h>
  18. #include <AppleEvents.h>
  19. #include <Errors.h>
  20. #include <Displays.h>            //    for Display Manager AppleEvent constants
  21. #include <OCEStandardMail.h>    //    for LetterSpec
  22.  
  23. #include "AppLib.h"
  24. #include "AppleEventHandling.h"
  25.  
  26. void InstallAppleEventHandlers(void)
  27. {
  28.     //    It’s probably more efficient to use a table to install these handlers…
  29.     
  30.     (void) AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOpenApplication), 0, false);
  31.     (void) AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(HandleOpenDocuments), 0, false);
  32.     (void) AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(HandlePrintDocuments), 0, false);
  33.     (void) AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuitApplication), 0, false);
  34.  
  35.     //    regardless of whether or not we have the display manager, go ahead and register an AppleEvent handler
  36.     (void) AEInstallEventHandler (kCoreEventClass, kAESystemConfigNotice, NewAEEventHandlerProc(HandleSystemConfigNotice), 0, false);
  37. }
  38.  
  39. OSErr CheckAppleEventForMissingParams(AppleEvent *pAppleEvent)
  40. {
  41.     DescType    aReturnedType;
  42.     Size        aActualSize;
  43.     OSErr        anErr;
  44.     
  45.     anErr = AEGetAttributePtr(pAppleEvent,keyMissedKeywordAttr,typeWildCard,
  46.                             &aReturnedType,nil,0,&aActualSize);
  47.     
  48.     switch (anErr)
  49.     {
  50.         case  errAEDescNotFound :    //    If we couldn’t find the error attribute
  51.             anErr = noErr;                        //        everything is ok, return noErr
  52.             break;
  53.         case noErr :                            //    We found an error attribute, so
  54.             anErr = errAEEventNotHandled;    //        tell the client we ignored the event
  55.             break;
  56.     //    Something else happened, return it
  57.     }
  58.     return anErr;    
  59. }
  60.  
  61. OSErr ForEachDocumentInList(AEDescList theDocumentList,EachDocumentProcPtr pDocumentProc,void * pDocumentParam)
  62. {
  63.     long                    aDocumentCount;
  64.     long                    aDocumentIndex;
  65.     DescType            aReturnedType;
  66.     Size                    aActualSize;
  67.     LetterDescriptor    aLetterDesc;
  68.     AEKeyword        aKeyword;
  69.     OSErr                anErr;
  70.  
  71.     anErr = AECountItems(&theDocumentList,&aDocumentCount);
  72.     if (anErr == noErr)
  73.         for (aDocumentIndex = 1; (anErr == noErr) && (aDocumentIndex <= aDocumentCount); ++aDocumentIndex)
  74.         {
  75.             //    What kind of document is it?
  76.             anErr = AESizeOfNthItem(&theDocumentList, aDocumentIndex, &aReturnedType,&aActualSize);
  77.             if (anErr == noErr)
  78.                 //    Is it an AOCE letter?
  79.                 if (aReturnedType == typeLetterSpec)
  80.                 {
  81.                     //    It’s a letter
  82.                     aLetterDesc.onDisk = false;
  83.                     anErr = AEGetNthPtr(&theDocumentList,aDocumentIndex,typeLetterSpec,&aKeyword,&aReturnedType,
  84.                                         (Ptr) &aLetterDesc.u.mailboxSpec, sizeof(aLetterDesc.u.mailboxSpec),&aActualSize);
  85.                 }
  86.                 else
  87.                 {
  88.                     //    It’s just a normal document file
  89.                     aLetterDesc.onDisk = true;
  90.                     anErr = AEGetNthPtr(&theDocumentList,aDocumentIndex,typeFSS,&aKeyword,&aReturnedType,
  91.                                         (Ptr) &aLetterDesc.u.fileSpec,sizeof(aLetterDesc.u.fileSpec),&aActualSize);
  92.                 }
  93.             if (anErr == noErr)
  94.                 (*pDocumentProc)(&aLetterDesc, pDocumentParam);
  95.         }
  96.     
  97.     return    anErr;
  98. }
  99.  
  100. pascal OSErr HandleOpenApplication(AppleEvent *pAppleEvent,AppleEvent * /*pReply*/,long /*theRefCon*/)
  101. {
  102.     OSErr    anErr;
  103.     
  104.     anErr = CheckAppleEventForMissingParams(pAppleEvent);
  105. #define        RESPONDTOOPENAPPEVENT        1
  106. #if        RESPONDTOOPENAPPEVENT
  107.     if (anErr == noErr)
  108.         anErr = OpenNewDocument();
  109. #endif    // RESPONDTOOPENAPPEVENT
  110.     return anErr;
  111. }
  112.  
  113. pascal OSErr HandleOpenDocuments(AppleEvent *pAppleEvent,AppleEvent * /*pReply*/,long /*theRefCon*/)
  114. {
  115.     OSErr                anErr;
  116.     AEDescList        aDocumentList;
  117.     
  118.     anErr = AEGetParamDesc(pAppleEvent, keyDirectObject, typeAEList, &aDocumentList);
  119.     if (anErr == noErr)
  120.         anErr = AEGetParamDesc(pAppleEvent, keyDirectObject, typeAEList, &aDocumentList);
  121.     if (anErr == noErr)
  122.         anErr = CheckAppleEventForMissingParams(pAppleEvent);
  123.     if (anErr == noErr)
  124.     {
  125.         anErr = ForEachDocumentInList(aDocumentList, &OpenDocument, nil);
  126.         (void) AEDisposeDesc(&aDocumentList);
  127.     }
  128.     return anErr;
  129. }
  130.  
  131. pascal OSErr HandlePrintDocuments(AppleEvent *pAppleEvent,AppleEvent * /*pReply*/,long /*theRefCon*/)
  132. {
  133.     OSErr                anErr;
  134.     AEDescList        aDocumentList;
  135. #if    qUseQuickDrawGX
  136.     Boolean                aIsDraggedToDesktopPrinter = false;
  137.     AEDescList        aDesktopPrinterList;
  138. #endif
  139.     
  140.     anErr = AEGetParamDesc(pAppleEvent, keyDirectObject, typeAEList, &aDocumentList);
  141.  
  142. #if    qUseQuickDrawGX
  143.     if (anErr == noErr)
  144.         if ( ! AEGetAttributeDesc(pAppleEvent, keyOptionalKeywordAttr, typeAEList, &aDesktopPrinterList))
  145.             aIsDraggedToDesktopPrinter = true;
  146. #endif
  147.     if (anErr == noErr)
  148.         anErr = CheckAppleEventForMissingParams(pAppleEvent);
  149.  
  150.     if (anErr == noErr)
  151. #if    qUseQuickDrawGX
  152.     {
  153.         FSSpec            aPrinterFSSpec;
  154.  
  155.         if (aIsDraggedToDesktopPrinter)
  156.         {
  157.             DescType            aReturnedType;
  158.             Size                    aActualSize;
  159.             AEKeyword        aKeyword;
  160.     
  161.             anErr = AEGetNthPtr(&aDesktopPrinterList,1, typeFSS,&aKeyword, &aReturnedType,
  162.                                 (Ptr) &aPrinterFSSpec, sizeof(FSSpec), &aActualSize);
  163.     
  164.             (void) AEDisposeDesc(&aDesktopPrinterList);
  165.         }
  166.         if (anErr == noErr)
  167.         {
  168.             anErr = ForEachDocumentInList(aDocumentList, &PrintDocument, (aIsDraggedToDesktopPrinter ? &aPrinterFSSpec : NULL));
  169.             (void)AEDisposeDesc(&aDocumentList);
  170.         }
  171.     }
  172. #else
  173.         anErr = ForEachDocumentInList(aDocumentList,&PrintDocument, nil);
  174. #endif
  175.  
  176.     return anErr;
  177. }
  178.  
  179. pascal OSErr HandleQuitApplication(AppleEvent *pAppleEvent,AppleEvent * /* pReply */,long /* theRefCon */)
  180. {
  181.     OSErr    anErr;
  182.     
  183.     anErr = CheckAppleEventForMissingParams(pAppleEvent);
  184.     if (anErr == noErr)
  185.     {    
  186.         gDone = QuitApplication();
  187.     
  188.         if ( ! gDone)
  189.             anErr = userCanceledErr;
  190.     }
  191.     return  anErr;
  192. }
  193.  
  194.  
  195. ////////////////////////////////////////////////////////////////////////
  196. //
  197. //    Display Manager Suite is “Under Construction”
  198. //
  199. //    To Do: Check ERS for Display Manager events
  200. pascal OSErr HandleSystemConfigNotice(AppleEvent *pAppleEvent,AppleEvent * /* pReply */,long /* theRefCon */)
  201. {
  202.     AEDescList    aDisplayList;
  203.     long        aDisplayCount;
  204.     long        aDisplayIndex;
  205.     OSErr        anErr;
  206.     
  207.     DebugStr((StringPtr) "\pGot a Display Manager Event!");
  208.     
  209.     if ((anErr = AEGetParamDesc(pAppleEvent,kAEDisplayNotice,typeAEList,&aDisplayList)) != noErr)
  210.         return anErr;
  211.     
  212.     if ((anErr = CheckAppleEventForMissingParams(pAppleEvent)) != noErr)
  213.         return anErr;
  214.     
  215.     if ((anErr = AECountItems(&aDisplayList,&aDisplayCount)) != noErr)
  216.         return anErr;
  217.  
  218.     for (aDisplayIndex = 1; aDisplayIndex<=aDisplayCount; aDisplayIndex++)
  219.     {
  220.         DebugStr((StringPtr) "\pProcessing a display");
  221.         //    Gather up all the old and new display rectangles into an oldDeskRegion and a newDeskRegion
  222.     }
  223.  
  224.  
  225.     //    run through all windows calling keeping all windows which were onscreen in the oldDeskRegion
  226.     //    onscreen in the new world. We should really be calling a method to do this so that windows
  227.     //    can individually deal with things in a manner which they can override.
  228.  
  229.  
  230.     return errAEEventNotHandled;    //    we really don’t handle this yet...
  231. }
  232.